<template>
  <!-- 币种管理页面 -->
  <div>
    <el-row class="currencyManage container">
      <el-col style="margin-right:10px;" class="content-left">
        <el-row class="handleBox">
          <el-col :span="18">
            <el-button type="primary" @click="addFunc()">新增</el-button>
            <el-button @click="editFunc()">编辑</el-button>
            <el-button @click="delFunc()">删除</el-button>

            <el-button @click="enable('1')">启用</el-button>
            <el-button @click="enable('2')">停用</el-button>
          </el-col>
          <el-col :span="6">
            <div class="handleIconBox">
              <el-popover
                placement="left-start"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="search_porper">
                  <el-form
                    :model="searchForm"
                    ref="searchForm"
                    label-width="100px"
                    label-position="right"
                    size="mini"
                  >
                    <el-form-item label="币种编码：">
                      <el-input
                        v-model="searchForm.currencyCode"
                        style="width:150px"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="币种名称：" prop="treeTypeName">
                      <el-input
                        v-model="searchForm.currencyName"
                        style="width:150px"
                      ></el-input>
                    </el-form-item>
                  </el-form>
                  <div style="text-align:center;padding:10px">
                    <el-button type="primary" size="mini">查询</el-button>
                    <el-button
                      size="mini"
                      @click="
                        (searchForm.currencyCode = ''),
                          (searchForm.currencyName = '')
                      "
                      >重置</el-button
                    >
                  </div>
                </div>
                <i
                  class="iconfont icon-sousuo"
                  title="搜索"
                  slot="reference"
                ></i>
              </el-popover>
              <el-popover
                placement="bottom"
                width="150"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="operation_checkbox">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox
                      v-for="item in textData"
                      :label="item.label"
                      :key="item.prop"
                      >{{ item.label }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
                <i
                  class="iconfont icon-liebiaoshezhi"
                  title="列表设置"
                  slot="reference"
                ></i>
              </el-popover>
            </div>
          </el-col>
        </el-row>
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          @row-click="authorizeFor"
        >
          <el-table-column
            width="55"
            type="selection"
            fixed="left"
          ></el-table-column>
          <el-table-column prop="currencyCode" label="编号" v-if="columnState[0]">
          </el-table-column>
          <el-table-column prop="currencyName" label="名称 " v-if="columnState[1]">
          </el-table-column>

          <el-table-column prop="currencyFlag" label="是否为主币种 " v-if="columnState[2]" 
          :formatter="row=>row.currencyFlag==1?'是':'否'">
          </el-table-column>
          <el-table-column
            prop="status"
            label="状态 "
            v-if="columnState[3]"
             :filter-method="(val, row) => row.status === val"
            :filters="[
              { text: '启用', value: '1' },
              { text: '注销', value: '0' }
            ]"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.status == 1">
                <i class="startState"></i><span>启用</span>
              </div>
              <div v-if="scope.row.status == 0">
                <i class="unStartState"></i><span style="color:red">注销 </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="creator"
            label="创建人"
            sortable
            v-if="columnState[4]"
          >
           
          </el-table-column>
          <el-table-column
            prop="createdTm"
            label="创建时间"
            sortable
            v-if="columnState[5]"
          >
            <template slot-scope="scope">
              <div>
                {{ timeFormat(scope.row.createdTm) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="modifier"
            label="修改人"
            sortable
            v-if="columnState[6]"
          >
          </el-table-column>
          <el-table-column
            prop="modifiedTm"
            label="修改时间"
            sortable
            v-if="columnState[7]"
          >
            <template slot-scope="scope">
              <div>
                {{ timeFormat(scope.row.modifiedTm) }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-size="15"
          :total="400"
        ></pagination>
      </el-col>
    </el-row>
    <el-dialog v-dialogDrag
      :title="saveTitles[saveFlg - 1]"
      :visible.sync="addOrEdit"
      top="2%"
      width="514px"
    >
      <el-form
        :model="saveForm"
        ref="saveForm"
        label-width="100px"
        label-position="left"
        :rules="saveFormRules"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="币种编号：" prop="currencyCode" required>
              <el-input
                v-model="saveForm.currencyCode"
                maxlength="40"
                :disabled="saveFlg==2"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="币种名称：" prop="currencyName" required>
              <el-input
                v-model="saveForm.currencyName"
                maxlength="40"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主币种标志：" prop="currencyFlag" required>
              <el-select
                v-model="saveForm.currencyFlag"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in mgrRoleIdList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :offset="1" :span="11">
            <el-form-item label="状态：" prop="status">
              <el-select
                v-model="saveForm.status"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in mgrRoleIdList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addOrEdit = false">确 定</el-button>
        <el-button @click="addOrEdit = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { publicManage } from "@/api/index";
import onlyOneSearch from "@/components/onlyOneSearch";
import moment from "moment";
export default {
  data() {
    return {
      userName: "", //右侧用户名
      userDo: "授权角色", // 右侧用户行为
      userDoFlag: 0,
      addOrEdit: false, // 新增/编辑/查看弹窗
      isModuleData: false, //同步模块数据弹窗
      serachMore: false,
      isProConfig: false, //工程表配置弹窗
      saveFlg: 1, // 1:新增，2：编辑，3查看,
      saveTitles: ["添加币种信息", "编辑币种信息"],
      // 机构树
      orgList: [
        {
          value: "01",
          label: "一级机构",
          children: [{ value: "001", label: "二级机构" }]
        }
      ],
      moduleDataForm: {},
      // 右侧树data
      treeData: [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1",
              children: [
                {
                  id: 9,
                  label: "三级 1-1-1"
                },
                {
                  id: 10,
                  label: "三级 1-1-2"
                }
              ]
            }
          ]
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1"
            },
            {
              id: 6,
              label: "二级 2-2"
            }
          ]
        },
        {
          id: 3,
          label: "一级 3",
          children: [
            {
              id: 7,
              label: "二级 3-1"
            },
            {
              id: 8,
              label: "二级 3-2"
            }
          ]
        }
      ],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 角色管理下拉框
      mgrRoleIdList: [
        {
          value: "选项1",
          label: "模块1"
        },
        {
          value: "选项2",
          label: "模块2"
        }
      ],
      // search机构树悬停提示
      searchOrgTip: "",
      // save表单机构树悬停提示
      saveOrgTip: "",
      searchForm: {
        username: "",
        page: 1,
        size: 10
      },
      saveForm: {},
      searchFF: {},
      saveFormRules: {
        userName: [
          { required: true, message: "该输入项为必输项", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "该输入项为必输项", trigger: "blur" }
        ],
        password: [
          { required: true, message: "该输入项为必输项", trigger: "blur" },
          { min: 6, max: 8, message: "密码长度在6-8之间", trigger: "blur" }
          // { validator: this.validPassword, trigger: "blur" },
        ],
        passwordStr: [
          { required: true, message: "该输入项为必输项", trigger: "blur" },
          { validator: this.validPassword, trigger: "blur" }
        ],
        orgId: [
          { required: true, message: "该输入项为必输项", trigger: "blur" }
        ],
      },
      checkList: [],
      // 列隐藏
      textData: [
        {  label: "币种编号" },
        {  label: "币种名称 " },
        { label: "是否为主币种" },
        { label: "状态" },
        {  label: "创建人 " },
        {  label: "创建时间" },
        { label: "修改人 " },
        { label: "修改时间 " },
      ],
      // 获取table的数据
      tableData: [{"creator":"sysadmin","createdTm":1557199289954,"modifier":"sysadmin","modifiedTm":1597046640933,"status":"1","currencyCode":"CNY","currencyName":"人民币","currencyFlag":"1","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1545891025000,"modifier":"sysadmin","modifiedTm":1594858680156,"status":"1","currencyCode":"HKD","currencyName":"港元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1546929601000,"modifier":"sysadmin","modifiedTm":1597025732947,"status":"1","currencyCode":"DFC","currencyName":"币种","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1557903154147,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"USD","currencyName":"美元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1557903179629,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"JPY","currencyName":"日元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1557903191929,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"EUR","currencyName":"欧元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1565245406607,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"GBP","currencyName":"英镑","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1565245969743,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"CAD","currencyName":"加拿大元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1565245978544,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"AUD","currencyName":"澳大利亚元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1565245996426,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"RUB","currencyName":"俄罗斯卢布","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1565246006055,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"SGD","currencyName":"新加坡元","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1587442121567,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"GPB","currencyName":"英镑","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1596675750015,"modifier":"sysadmin","modifiedTm":1596675782586,"status":"1","currencyCode":"CNN","currencyName":"苏币","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1597025289904,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"RMB","currencyName":"人民币","currencyFlag":"0","currencyDescribe":null,"currencySign":null},{"creator":"sysadmin","createdTm":1601189478416,"modifier":null,"modifiedTm":null,"status":"1","currencyCode":"RRR","currencyName":"RRR","currencyFlag":"0","currencyDescribe":null,"currencySign":null}]
    };
  },
  components: {
    onlyOneSearch
  },
  created() {
    this.checkListInit();
    console.log(this.tableData, "模拟数据");
  },
  computed: {
    columnState() {
      console.log(this.checkList);
      let arr = [],
        arr1 = this.textData,
        arr2 = this.checkList;
      for (let i = 0; i < arr1.length; i++) {
        let flg = false;
        for (let j = 0; j < arr2.length; j++) {
          if (arr1[i].label === arr2[j]) {
            flg = true;
          }
        }
        arr.push(flg);
      }
      return arr;
    }
  },
  mounted() {
    // this.getList();
  },
  watch: {},
  methods: {
    checkListInit() {
      this.checkList = this.textData.map(v => {
        return v.label;
      });
    },
    searchFunc(e) {
      console.log(this.searchForm, e);
    },
    addFunc() {
      this.saveFlg = 1;
      for (let k in this.saveForm) {
        this.saveForm[k] = "";
      }
      this.addOrEdit = true;
    },
    editFunc() {
      this.saveFlg = 2;
      let selection = this.$refs.multipleTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据进行修改");
      }
      let row = selection[0];
      let form = { ...row };
      this.saveForm = form;
      this.addOrEdit = true;
    },
    delFunc() {
      let selection = this.$refs.multipleTable.selection;
      if (selection.length < 1) {
        return this.$message.warning("请选择要删除的数据");
      }
      this.$confirm("你确定要删除选中币种吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message.success("删除成功");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    enable(data) {
      let val = data == "1" ? "启用" : "停用";
      let selection = this.$refs.multipleTable.selection;
      if (selection.length < 1) {
        return this.$message.warning("请选择要" + val + "的币种");
      }
      this.$confirm(`你确定要${val}所选币种吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message.success(val + "成功");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消" + val
          });
        });
    },
    handleNodeClick() {
      let nodes = this.$refs.configTree.getCheckedNodes();
      console.log(nodes);
    },
    caliberProjectNameFormatter(row) {
      return row.caliberProject.caliberProjectName;
    },
    getList() {
      publicManage.query("我的参数").then(res => {
        console.log(res);
      });
    },
    // 点击表格右侧显示为xx授权
    authorizeFor(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
      this.userName = row.roleName;
    },
    // 重新加载
    reload() {
      this.isModuleData = true;
    },
    timeFormat(val) {
      return moment(val).format("YYYY-MM-DD");
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

<style lang="scss" scoped>
.dialogSearchBox {
  header {
    text-align: center;
    span {
      margin: 0 20px;
    }
  }
}
</style>